//合作科目控制。
Ext.define("ES.controller.CooperationCatalogController",{
    extend:"Ext.app.Controller",
    models:["CooperationModel"],
    stores:["CooperationCatalogInfoStore","CooperationStore","CooperationCatalogTreeStore"],
    views:["cooperationcatalog.CooperationCatalogList","cooperationcatalog.CooperationCatalogEdit"],
    requires:["ES.common"],
    init:function(){
         this.control({
             "cooperationCatalogTabList":{
                afterrender:function(cmp){
                     var btn = cmp.down("toolbar button[action=btnQuery]");
                     this.onSearch(btn);
                },
                itemdblclick:this.onEdit
             },
             "cooperationCatalogTabList button[action=btnQuery]":{
                  click:this.onSearch
             },
             "cooperationCatalogTabList button[action=btnAdd]":{
                  click:this.onAdd
             },
             "cooperationCatalogTabList button[action=btnDelete]":{
                  click:this.onDelete
             },
             "cooperationCatalogEdit button[action=btnSave]":{
                  click:this.onSave
             },
             "cooperationCatalogEdit>form>fieldset>treepanel":{
                 checkchange:function(node,checked,opts){
                     if(node.hasChildNodes()){
                          node.cascadeBy(function(child){
                                child.set("checked",checked);
                            });
                     }
                     var p = null;
                     if(!checked && (p = node.parentNode) != null){
                         while(p != null){
                             p.set("checked",checked);
                             p = p.parentNode;
                         }
                     }
                 }
             },
             "cooperationCatalogEdit button[action=btnCancel]":{
                  click:function(btn){
                     var win = btn.up("cooperationCatalogEdit");
                     if(win){
                         win.close();
                     }
                }
             }
         })
    },
    onSearch:function(btn){
        var toolbar =  btn.ownerCt;
        var grid = toolbar.ownerCt;       
        var st = grid.getStore();
        st.loadPage(1,{
            params:{
                cooperationName:toolbar.getComponent(1).getValue(),
                catalogName:toolbar.getComponent(3).getValue()
            }
        });
    },
    onAdd:function(){
         var win = Ext.widget("cooperationCatalogEdit");
         var store = this.getCooperationStoreStore();
         if(store){
             store.load();
             var tv = win.down("treepanel");
             if(tv){
                 tv.getStore().load({
                     params:{cooperationID:""}
                 });
             }
         }
         win.show();
    },
    onEdit:function(view, record, item,index,e,opt){
         var win = Ext.widget("cooperationCatalogEdit");
         var store = this.getCooperationStoreStore();
         win.show();
         if(store){
             var el = win.getEl();
             el.mask("正在加载数据...");
             
              store.load({
                  callback: function(records, operation, success){
                      if(success){
                          var cb = win.down("combo");
                          if(cb){
                              cb.setValue(record.raw.cooperationID);
                          }
                          var tv = win.down("treepanel");
                          if(tv){
                              tv.getStore().load({
                                  params:{cooperationID:record.raw.cooperationID}
                              });
                          }
                      }
                      el.unmask();
                  }
              });
         }
    },
    onSave:function(btn){
        var win = btn.up("cooperationCatalogEdit");
        var form = win.down("form").getForm();
        var treepanel = win.down("treepanel");
        if(form.isValid() && treepanel){
            var data = form.getValues();
            var checks = treepanel.getChecked();
            if(checks.length>0){
                var items = [];
                Ext.Array.each(checks,function(val){
                    items.push(val.raw.id);
                });
                var store = this.getCooperationCatalogInfoStoreStore();
                store.add({cooperationID:data.cooperationID,catalogID:items.join(",")});
                store.sync();
                win.close();
            }else{
                Ext.MessageBox.show({
                    title:"错误信息",
                    msg:"请至少选中一个科目！"
                });
            }
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                        Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                        var st = grid.getStore();
                        Ext.Array.each(data, function(record){
                            st.remove(record);
                        })
                        st.sync();
                }
            }
        });
    }
})